﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;

namespace EC.Admin.Controllers
{
    using EC.Application.Interface;

    using EC.Entity;
    using EC.Entity.Enum;
    using EC.Entity.Query;
    using EC.Entity.Request.PaymentRecord;
    using EC.Util;

    /// <summary>
    /// 付款
    /// </summary>
    public class PaymentRecordController : BaseController
    {
        #region 注入
        /// <summary>
        /// 视频数据接口
        /// </summary>
        private readonly PaymentRecordService _paymentRecordService;

        /// <summary>
        /// 用户组数据接口
        /// </summary>
        private readonly UserGroupService _userGroupService;

        /// <summary>
        /// 商户数据接口
        /// </summary>
        private readonly MerchantService _merchantService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="labelService"></param>
        public PaymentRecordController(PaymentRecordService paymentRecordService, UserGroupService userGroupService, MerchantService merchantService, LoginService loginService) : base(loginService)
        {
            _paymentRecordService = paymentRecordService;
            _userGroupService = userGroupService;
            _merchantService = merchantService;
        }

        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.PaymentRecord0001)]
        public IActionResult Index()
        {

            ViewBag.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            ViewBag.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");

            ViewBag.YesterdayStartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
            ViewBag.YesterdayEndTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 23:59:59");

            ViewBag.MonthStartTime = DateUtil.FirstDayOfMonth(DateTime.Now).ToString("yyyy-MM-dd 00:00:00");
            ViewBag.MonthEndTime = DateUtil.LastDayOfMonth(DateTime.Now).ToString("yyyy-MM-dd 23:59:59");

            var merchantIds = new List<int>() { 0 };
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    merchantIds.Add(Convert.ToInt32(item));
                }
            }

            ViewBag.Merchants = _merchantService.GetMerchantByIds(merchantIds);

            return View();
        }
        #endregion

        #region 审核
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.PaymentRecord0002)]
        [HttpPost]
        public JsonResult Audit(AuditRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = MerchantId;
            request.Status = (int)PaymentRecordStatus.付款中;

            var response = _paymentRecordService.Audit(request);

            return Json(response);
        }
        #endregion

        #region 确认付款
        /// <summary>
        /// 确认付款
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.PaymentRecord0003)]
        [HttpGet]
        public IActionResult Confirm(int id)
        {
            var model = _paymentRecordService.Get(id);

            return View(model);
        }

        /// <summary>
        /// 确认付款
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.PaymentRecord0003)]
        [HttpPost]
        public JsonResult Confirm(ConfirmRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = MerchantId;
            request.Status = (int)PaymentRecordStatus.付款成功;

            var response = _paymentRecordService.Confirm(request);

            return Json(response);
        }
        #endregion

        #region 取消
        /// <summary>
        /// 取消
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.PaymentRecord0004)]
        [HttpGet]
        public IActionResult Cancel(int id)
        {
            var model = _paymentRecordService.Get(id);

            return View(model);
        }

        /// <summary>
        /// 取消付款
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.PaymentRecord0004)]
        [HttpPost]
        public JsonResult Cancel(CancelRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = MerchantId;
            request.Status = (int)PaymentRecordStatus.取消付款;

            var response = _paymentRecordService.Cancel(request);

            return Json(response);
        }
        #endregion

        #region 确认失败
        /// <summary>
        /// 撤回
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.PaymentRecord0005)]
        [HttpGet]
        public IActionResult Undo(int id)
        {
            var model = _paymentRecordService.Get(id);

            return View("Undo", model);
        }

        /// <summary>
        /// 撤回
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.PaymentRecord0005)]
        [HttpPost]
        public JsonResult Undo(CancelRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = MerchantId;
            request.Status = (int)PaymentRecordStatus.取消付款;

            var response = _paymentRecordService.Undo(request);

            return Json(response);
        }
        #endregion

        #region 重置
        /// <summary>
        /// 重置
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.PaymentRecord0006)]
        [HttpPost]
        public JsonResult Reset(int id)
        {
            var response = _paymentRecordService.Reset(id);

            return Json(response);
        }
        #endregion

        #region 还原成功
        /// <summary>
        /// 还原成功
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.PaymentRecord0006)]
        [HttpPost]
        public JsonResult ReturnSuccess(int id)
        {
            var response = _paymentRecordService.ReturnSuccess(new CancelRequest()
            {
                Id = id,
                UserId = UserId
            });

            return Json(response);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>

        private bool IsDate(string date)
        {
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.PaymentRecord0001)]
        public JsonResult PaymentRecordQuery(PaymentRecordQuery query)
        {
            query.UserId = UserId;
            query.MerchantIds = new List<int>() { -1 };
            query.UserPermission = UserPermissions;

            if(query.MerchantId.HasValue)
            {
                query.MerchantIds.Add(query.MerchantId.Value);
            }
            else
            {
                //获取公司
                var merchant = _merchantService.Get(MerchantId);
                //验证
                if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
                {
                    var ids = merchant.ProxyId.Split(',');
                    foreach (var item in ids)
                    {
                        query.MerchantIds.Add(Convert.ToInt32(item));
                    }
                }
            }

            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            var list = _paymentRecordService.GetPager(query);

            return Json(list);
        }

        /// <summary>
        /// 付款订单导出
        /// </summary>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.PaymentRecord0001)]
        public JsonResult PaymentRecordExport(PaymentRecordQuery query)
        {
            query.UserPermission = UserPermissions;
            query.MerchantIds = new List<int>() { -1 };
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    query.MerchantIds.Add(Convert.ToInt32(item));
                }
            }

            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            var list = _paymentRecordService.GetMerchantExportPager(query);
            list.StatusCode = $"{query.StartTime}-{query.EndTime}";
            return Json(list);
        }
        #endregion
    }
}
